﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Security.Principal;
using System.Data.Objects;
using System.Linq.Expressions;

namespace IRC.IRepairClouds.DataLayer
{
    partial class EUser
    {
        public static Expression<Func<EUser, bool>> SelectorByUserId(Guid userId)
        {
            return i => i.ID == userId;
        }

        public static Expression<Func<EUser, bool>> SelectorByLogin(string login)
        {
            return i => i.Login.ToLower() == login.ToLower();
        }

        public static string Get()
        {
            string get = ConfigurationManager.AppSettings["EUser.Get"];
            switch (get) {
                case "System.Web.HttpContext.Current.User.Identity.Name": {
                        if (System.Web.HttpContext.Current != null) {
                            IPrincipal user = System.Web.HttpContext.Current.User;
                            if (user != null && user.Identity != null && user.Identity.IsAuthenticated) {
                                return System.Web.HttpContext.Current.User.Identity.Name.TrimEnd();
                            }
                        }
                    }
                    break;
                case "System":
                    return "System";
            }
            return null;
        }

        public static bool IsLoggedIn
        {
            get { return Get() != null; }
        }
    }
}
